/*********************
* Author:Dzlua
* QQ:505544956
* time:2016/12/01
*/
#include "LuaLabel.h"
#include "../../utilities/Log.h"

#ifdef EDUI_USE_LUAPLUS

EDUI_NAMESPACE_BEGIN

#define CObjPtr (Label::ToLabel(m_cobj))

void* LuaLabel::GetInterface(void* data)
{
    if (!data) return 0;
    LuaLabel* label = new LuaLabel((Label*)data);
#ifdef EDUI_TEST_NEW_DELETE
    ++m_newdeleteNum;
    Log("[LuaLabel] new LuaLabel : %d, %d", label, data);
#endif
    return label;
}

void LuaLabel::Release()
{
#ifdef EDUI_TEST_NEW_DELETE
    --m_newdeleteNum;
    Log("[LuaLabel] delete LuaLabel : %d, %d, %s", this, m_cobj, this->GetName());
#endif
    delete this;
}

LuaLabel::LuaLabel(Label* label) : LuaControl(label)
{}

LuaLabel::~LuaLabel()
{}

//----------------------------------------//

int LuaLabel::GetFont(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    bool bWindow = true;
    if (args[2].IsBoolean()) bWindow = args[2].GetBoolean();
    IFont* font = CObjPtr->GetFont(bWindow);
    LuaObject obj = LuaHelperNS::GetFromObjs(L, font);
    obj.Push(L);
    return 1;
}

int LuaLabel::SetFont(LuaState *L)
{
    LuaStack args(L);
    bool bRet = false;
    //cause RegisterObjectFunctor,the first param must be args[2]
    IFont* font = (IFont*) LuaHelperNS::GetCObjectData(args[2]);
    bRet = CObjPtr->SetFont(font);
    L->PushBoolean(bRet);
    return 1;
}

void LuaLabel::SetText(const char* text)
{
    CObjPtr->SetText(text);
}

const char* LuaLabel::GetText()
{
    return CObjPtr->GetText();
}

void LuaLabel::SetTranslateText(bool bTranslate)
{
    CObjPtr->SetTranslateText(bTranslate);
}

bool LuaLabel::IsTranslateText()
{
    return CObjPtr->IsTranslateText();
}

const char* LuaLabel::GetTranslateText()
{
    return CObjPtr->GetTranslateText();
}

int LuaLabel::GetTextAlign(LuaState *L)
{
    AlignType aligntype = CObjPtr->GetTextAlign();
    LuaObject obj = LuaBaseNS::ToLuaObj(L, aligntype);
    obj.Push(L);
    return 1;
}

void LuaLabel::SetTextAlign(const char* align)
{
    AlignType aligntype = ParserAlign(align);
    CObjPtr->SetTextAlign(aligntype);
}

int LuaLabel::GetTextPadding(LuaState *L)
{
    Rect1 rc1 = CObjPtr->GetTextPadding();
    LuaObject obj = LuaBaseNS::ToLuaObj(L, &rc1);
    obj.Push(L);
    return 1;
}

bool LuaLabel::SetTextPadding(int l, int t, int r, int b)
{
    Rect1 rc1(l, t, r, b);
    return CObjPtr->SetTextPadding(&rc1);
}

void LuaLabel::SetAutoWidth(bool bauto)
{
    CObjPtr->SetAutoWidth(bauto);
}

bool LuaLabel::IsAutoWidth()
{
    return CObjPtr->IsAutoWidth();
}

void LuaLabel::SetAutoHeight(bool bauto)
{
    CObjPtr->SetAutoHeight(bauto);
}

bool LuaLabel::IsAutoHeight()
{
    return CObjPtr->IsAutoHeight();
}

int LuaLabel::GetFixedSize(LuaState *L)
{
    Size sz = CObjPtr->GetFixedSize();
    LuaObject obj = LuaBaseNS::ToLuaObj(L, &sz);
    obj.Push(L);
    return 1;
}

int LuaLabel::GetFixedRect(LuaState *L)
{
    Rect rc = CObjPtr->GetFixedRect();
    LuaObject obj = LuaBaseNS::ToLuaObj(L, &rc);
    obj.Push(L);
    return 1;
}

//----------------------------------------//

void LuaLabel::Register(LuaState *L)
{
    LuaObject obj = LuaHelperNS::AddToMetas(L, "Label", "Control");
    obj.SetObject("__index", obj);
    obj.RegisterDirect("__NewCObj__", &LuaLabel::GetInterface);

    obj.RegisterObjectFunctor("GetFont", &LuaLabel::GetFont);
    obj.RegisterObjectFunctor("SetFont", &LuaLabel::SetFont);
    obj.RegisterObjectDirect("SetText", (LuaLabel*)0, &LuaLabel::SetText);
    obj.RegisterObjectDirect("GetText", (LuaLabel*)0, &LuaLabel::GetText);
    obj.RegisterObjectDirect("SetTranslateText", (LuaLabel*)0, &LuaLabel::SetTranslateText);
    obj.RegisterObjectDirect("IsTranslateText", (LuaLabel*)0, &LuaLabel::IsTranslateText);
    obj.RegisterObjectDirect("GetTranslateText", (LuaLabel*)0, &LuaLabel::GetTranslateText);
    obj.RegisterObjectFunctor("GetTextAlign", &LuaLabel::GetTextAlign);
    obj.RegisterObjectDirect("SetTextAlign", (LuaLabel*)0, &LuaLabel::SetTextAlign);
    obj.RegisterObjectFunctor("GetTextPadding", &LuaLabel::GetTextPadding);
    obj.RegisterObjectDirect("SetTextPadding", (LuaLabel*)0, &LuaLabel::SetTextPadding);
    obj.RegisterObjectDirect("SetAutoWidth", (LuaLabel*)0, &LuaLabel::SetAutoWidth);
    obj.RegisterObjectDirect("IsAutoWidth", (LuaLabel*)0, &LuaLabel::IsAutoWidth);
    obj.RegisterObjectDirect("SetAutoHeight", (LuaLabel*)0, &LuaLabel::SetAutoHeight);
    obj.RegisterObjectDirect("IsAutoHeight", (LuaLabel*)0, &LuaLabel::IsAutoHeight);
    obj.RegisterObjectFunctor("GetFixedSize", &LuaLabel::GetFixedSize);
    obj.RegisterObjectFunctor("GetFixedRect", &LuaLabel::GetFixedRect);
}

#undef CObjPtr

EDUI_NAMESPACE_END

#endif